<extend name="Base/base" />
<block name="button">	
	<button class="btn btn-primary" type="submit" form="form"><i class="fa fa-save"></i> {:L('text_save')}</button>
</block>
<block name="body">
	<if condition="$msg">
		<div class="alert alert-danger alert-dismissible fade in" role="alert">
		      <button type="button" class="close" data-dismiss="alert" aria-label="Close"><span aria-hidden="true">×</span></button>
		      {$msg.warning}
		    </div>
	</if>
	<div class="panel panel-default">
		<div class="panel-heading">
			<h4><i class="fa fa-pencil fa-fw"></i>  {$heading_title}</h4>
		</div>
		<div class="panel-body">
			<form action="" method="post" id="form" class="form-horizontal">
			<ul class="nav nav-tabs">
				<li class="active"><a href="#tab-data" data-toggle="tab">{:L('tab_data')}</a></li>
				<li><a href="#tab-text" data-toggle="tab">{:L('tab_text')}</a></li>
			</ul>
			<div class="tab-content">
				<div class="tab-pane active" id="tab-data">
					<div class="form-group required">
						<label for="input-filename" class="col-sm-2 control-label">{:L('entry_filename')} </label>
						<div class="col-sm-10">
							<div class="input-group">
						      <input type="text" readonly="readonly" class="form-control" placeholder="{:L('entry_filename')}" id="input-filename" name="filename" value="{$download.filename|default=''}">
						      <span class="input-group-btn">
						        <button id="button-upload" class="btn btn-primary" data-loading-text="{:L('text_loading')}" type="button"><i class="fa fa-upload"></i> {:L('button_upload')}</button>
						      </span>
						    </div>
						</div>
					</div>
					<div class="form-group required">
						<label for="input-mask" class="col-sm-2 control-label">{:L('entry_mask')}  <span class="text-primary" data-toggle="tooltip" title="" data-original-title="{:L('help_mask')}"><i class="fa fa-question-circle"></i></span></label>
						<div class="col-sm-10">
							<input type="text" name="mask" class="form-control" id="input-mask" placeholder="{:L('entry_mask')}" value="{$download.mask|default=''}">
						</div>
					</div>
				</div>
				<div class="tab-pane" id="tab-text">
					<ul class="nav nav-tabs">
						<foreach name="languages" key="key" item="language">
							<li class="{$key?'':'active'}">
								<a href="#language{$language.language_id}" data-toggle="tab"><img src="__PUBLIC__/Images/flags/{$language.image}">  {$language.name}</a>
							</li>
						</foreach>
					</ul>
					<div class="tab-content">
						<foreach name="languages" key="key" item="language">
							<div class="tab-pane {$key?'':'active'}" id="language{$language.language_id}">
								<div class="form-group required">
									<label for="input-name{$language.language_id}" class="col-sm-2 control-label">{:L('entry_name')}</label>
									<div class="col-sm-10">
										<input id="input-name{$language.language_id}" type="text" value="{$download['download_description'][$language['language_id']]['name']|default=''}" name="download_description[{$language.language_id}][name]" class="form-control" placeholder="{:L('entry_name')}">
										<if condition="isset($msg['name'][$language['language_id']])">
										{$msg['name'][$language['language_id']]|danger_msg=###}
										</if>
									</div>
								</div>
							</div>
						</foreach>
					</div>	
				</div>
			</div>
					

								
			</form>
		</div>		
	</div>
</block>
<block name="script">
<script type="text/javascript">
	$('#button-upload').on('click', function() {
		$('#form-upload').remove();
		
		$('body').prepend('<form enctype="multipart/form-data" id="form-upload" style="display: none;"><input type="file" name="file" /></form>');

		$('#form-upload input[name=\'file\']').trigger('click');
		
		if (typeof timer != 'undefined') {
	    	clearInterval(timer);
		}
		
		timer = setInterval(function() {
			if ($('#form-upload input[name=\'file\']').val() != '') {
				clearInterval(timer);		
				
				$.ajax({
					url: '{:U("upload")}',
					type: 'post',		
					dataType: 'json',
					data: new FormData($('#form-upload')[0]),
					cache: false,
					contentType: false,
					processData: false,		
					beforeSend: function() {
						$('#button-upload').button('loading');
					},
					complete: function() {
						$('#button-upload').button('reset');
					},	
					success: function(json) {
						if (json['error']) {
							alert(json['error']);
						}
									
						if (json['success']) {
							alert(json['success']);
							
							$('input[name=\'filename\']').attr('value', json['filename']);
							$('input[name=\'mask\']').attr('value', json['mask']);
						}
					},			
					error: function(xhr, ajaxOptions, thrownError) {
						alert(thrownError + "\r\n" + xhr.statusText + "\r\n" + xhr.responseText);
					}
				});
			}
		}, 500);
	});
</script>
</block>
